﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Adding
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.SessionState;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class ICM_MasContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            selectAuthen();
        }

        public void selectAuthen()
        {
            StringBuilder _txt = new StringBuilder();
            DataSet ds = new DataSet();

            string _query, _username, _password;
            try
            {

                _query = @"
                      SELECT
                          [user_code]
                          ,[user_password]
                      FROM [sys_sc_users]
                      where [emp_seq] = " + Session["EmpSeq"] + @"
                      AND [record_status] != 'D'
            ";
                ds = Db.exe(_query, "Authen");


                _username = ds.Tables["Authen"].Rows[0]["user_code"].ToString();
                _password = ds.Tables["Authen"].Rows[0]["user_password"].ToString();
                string tmp_username, tmp_password, tmp_url;
                string txtUrldb = GetUrlConnection();
                tmp_username = base64Encode(_username);
                tmp_password = base64Encode(_password);
                tmp_url = base64Encode(txtUrldb);

                _txt.Append(@"
                <span id='AuthenUser' style='display:none'>" + tmp_username + @"</span>
                <span id='AuthenPass' style='display:none'>" + tmp_password + @"</span>
                <span id='AuthenUrlDb' style='display:none'>" + tmp_url + @"</span>
            ");

                Response.Write(_txt);
            }
            catch (Exception e)
            {
                throw new Exception("Error in connection Database to selectAuthen" + e.Message);
            }
        }

        public class Db : IRequiresSessionState
        {
            //connection Database
            public static SqlConnection getConnect()
            {
                string dbConn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
                SqlConnection _con = new SqlConnection(dbConn);
                return _con;
            }

            //Query in Dataset to function
            public static DataSet exe(string _query, string _name = "name auto")
            {
                SqlDataAdapter _adp = new SqlDataAdapter(_query, getConnect());//connect
                DataSet _ds = new DataSet();
                try
                {
                    if (_name == "name auto")
                    {
                        _adp.Fill(_ds);
                    }
                    else
                    {
                        _adp.Fill(_ds, _name);
                    }
                    _adp.Dispose();
                }
                catch (Exception e)
                {
                    throw new Exception("Error in connecting DataSet" + e.Message);
                    //_adp.Dispose();
                }
                return _ds;
            }
        }

        public string base64Encode(string data)
        {
            try
            {
                byte[] encData_byte = new byte[data.Length];
                encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
                string encodedData = Convert.ToBase64String(encData_byte);
                return encodedData;
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Encode" + e.Message);
            }
        }

        public string GetUrlConnection()
        {
            string dbConn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            string[] sAryOne = dbConn.Split(';');
            string strOne = sAryOne[0];
            string[] sAryTwo = strOne.Split('\\');
            string strTwo = sAryTwo[0];
            string[] sAryTree = strTwo.Split('=');
            string nameDb = sAryTree[1];
            return nameDb;

        }

    }
}